package com.bluebench.bigul.domain.graphics;

import java.util.ArrayList;
import java.util.List;

import com.bluebench.bigul.exception.GraphicInitializationException;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfShading;
import com.itextpdf.text.pdf.PdfShadingPattern;

public abstract class Graphic{
	
	public static final int RIGHT=0;
	public static final int LEFT=1;
	public static final int TOP=2;
	public static final int BOTTOM=3;
	
	protected static PdfContentByte canvas;
	protected List<Point> vertices;
	
	public Graphic() {
		vertices=new ArrayList<Point>();
	}
	
	public Graphic(PdfContentByte canvas) {
		vertices=new ArrayList<Point>();
		this.canvas = canvas;
	}


	/*static void initialize(PdfContentByte pdfContent){
		Graphic.canvas = pdfContent;
		canvas.saveState();
	}*/
	public static double convertToRadian(double degrees){
		return (Math.toRadians(degrees));
	}
	
	public abstract Graphic build() throws GraphicInitializationException;
	
	protected abstract boolean validate();
	
	public Graphic border(BaseColor bc){
		canvas.setLineWidth(1);
		canvas.setColorStroke(bc);
		canvas.closePathStroke();
		return this;
		
	}
	
	public Graphic border(BaseColor bc, float borderWidth){
		canvas.setLineWidth(borderWidth);
		canvas.setColorStroke(bc);
		canvas.closePathStroke();
		return this;
		
	}	
	
	public Graphic fill(BaseColor bc){
		canvas.setColorFill(bc);
		canvas.fill();	
		return this;
	}
	
	public Graphic borderAndFill(BaseColor borderColor, BaseColor fillColor){
		canvas.setColorStroke(borderColor);
		canvas.setColorFill(fillColor);
		canvas.closePathFillStroke();
		return this;
	}
	
	public Graphic borderAndShade(BaseColor borderColor, Shading shading){
		canvas.setColorStroke(borderColor);
		PdfShading pdfShading = shading.getItextShading(canvas.getPdfWriter());
		PdfShadingPattern shadePattern = new PdfShadingPattern(pdfShading);
		canvas.setShadingFill(shadePattern);
        canvas.closePathFillStroke();
		return this;
	}
	
	public Graphic shade(Shading shading){
		PdfShading pdfShading = shading.getItextShading(canvas.getPdfWriter());
		PdfShadingPattern shadePattern = new PdfShadingPattern(pdfShading);
		canvas.setShadingFill(shadePattern);
        canvas.fill();
		return this;
	}
	
	
	public Graphic borderAndFill(BaseColor borderColor, float borderWidth, BaseColor fillColor){
		canvas.setColorStroke(borderColor);
		canvas.setLineWidth(borderWidth);
		canvas.setColorFill(fillColor);
		canvas.closePathFillStroke();
		return this;
	}
	
	//Graphic rotate(float rotation);
	protected abstract Graphic concentric(float distance, boolean in);
	
	public abstract Graphic copy(float distance, int direction);

	public List<Point> getVertices() {
		return vertices;
	}

	public void setVertices(List<Point> vertices) {
		this.vertices = vertices;
	}
	
	public void addVertex(Point p){
		this.vertices.add(p);
	}
}
